package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-06
 */
@Api(description = "课程管理")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "查询所有课程")
    @GetMapping("/getCourseInfo")
    //TODO 实现带条件带分页的课程查询
    public R getCourseInfo(){
        List<EduCourse> courseList = courseService.list(null);
        return R.ok().data("courseList",courseList);
    }

    @GetMapping("/getCoursePage/{page}/{limit}")
    @ApiOperation(value = "分页查询课程信息")
    public R getCoursePage(@PathVariable Long page,
                            @PathVariable Long limit) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.page(pageParam, null);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        //方式一
       /* Map<String,Object>map=new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return R.ok().data(map);*/
        //方式二
        return R.ok().data("records", records).data("total", total);
    }
    @ApiOperation(value = "添加课程相关信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.addCourseInfo(courseInfoForm);
        return R.ok().data("courseId", courseId);
    }

    @ApiOperation(value = "根据课程id查询课程相关信息")
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId) {
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(courseId);
        return R.ok().data("courseInfoForm", courseInfoForm);
    }

    @ApiOperation(value = "修改课程相关信息")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        //不需要返回id，数据回显的时候自带id
        courseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }

    @ApiOperation(value = "根据课程id查询课程发布相关信息")
    @GetMapping("/getCoursePublishById/{courseId}")
    public R getCoursePublishById(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishById(courseId);
        return R.ok().data("coursePublishVo", coursePublishVo);
    }


    @ApiOperation(value = "根据课程id发布课程")
    @PostMapping("/PublishCourseById/{courseId}")
    public R PublishCourseById(@PathVariable String courseId){
        //根据id查询课程信息
        EduCourse eduCourse = courseService.getById(courseId);
        //更新字段
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }



    @ApiOperation(value = "根据课程id删除课程相关")
    @DeleteMapping("/delCourseInfoById/{courseId}")
    public R delCourseInfoById(@PathVariable String courseId){
        courseService.delCourseInfoById(courseId);
        return R.ok();
    }


}

